package com.video.myvideo;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.video.myvideo.activity.SecondActivity;

import java.util.LinkedHashMap;

import cn.jzvd.JZDataSource;
import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private JzvdStd mJzvdStd;
    private Button mOrientation,
            mRotationAndVideoSize, mCustomMediaPlayer, mPreLoading, mScreenRotate;
    private Button serverCn, serverUs,btGoFirst,btGoSecond;
    private Jzvd.JZAutoFullscreenListener mSensorEventListener;
    private SensorManager mSensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mJzvdStd = findViewById(R.id.jz_video);
        mOrientation = findViewById(R.id.orientation);
        mRotationAndVideoSize = findViewById(R.id.rotation_and_videosize);
        mCustomMediaPlayer = findViewById(R.id.custom_mediaplayer);
        mPreLoading = findViewById(R.id.preloading);
        mScreenRotate = findViewById(R.id.screen_rotate);
        serverCn = findViewById(R.id.server_cn);
        serverUs = findViewById(R.id.server_us);
        btGoFirst = findViewById(R.id.goto_firstac);
        btGoSecond = findViewById(R.id.goto_secondac);

        mSensorManager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
        mSensorEventListener = new Jzvd.JZAutoFullscreenListener();

        btGoFirst.setOnClickListener(this);
        btGoSecond.setOnClickListener(this);

        initVideo();

    }

    private void initVideo() {
        LinkedHashMap map = new LinkedHashMap();
        String proxyUrl = ApplicationDemo.getProxy(MainActivity.this).getProxyUrl(UrlsKt.getCndVideos()[0]);
        Log.i("BaseFragmentVideoUrl", "onActivityCreated: "+proxyUrl);
        map.put("高清", proxyUrl);
        map.put("标清", UrlsKt.getCndVideos()[1]);
        map.put("普清", UrlsKt.getCndVideos()[2]);
        JZDataSource jzDataSource = new JZDataSource(map, "饺子起立");
        jzDataSource.looping = true;
        jzDataSource.currentUrlIndex = 2;
        jzDataSource.headerMap.put("key", "value");//header
        mJzvdStd.setUp(jzDataSource
                , JzvdStd.SCREEN_NORMAL);

        Jzvd.PROGRESS_DRAG_RATE = 2f;//设置播放进度条手势滑动阻尼系数
        Glide.with(this).load(UrlsKt.getCndThumbnail()[0]).into(mJzvdStd.posterImageView);


    }
    @Override
    public void onResume() {
        super.onResume();
        Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(mSensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        //home back
        Jzvd.goOnPlayOnResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(mSensorEventListener);
        JZUtils.clearSavedProgress(this, null);
        //home back
        Jzvd.goOnPlayOnPause();
    }

    @Override
    public void onClick(View view) {

        switch(view.getId()){
            case R.id.goto_firstac:
                startActivity(new Intent(MainActivity.this,FirstActivity.class));
                break;
            case R.id.goto_secondac:
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
                break;

            default:

                break;
        }

    }



    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();

    }
}